﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
using System.Security.Claims;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

builder.Services.AddAuthentication(options => 
{
    options.DefaultScheme = "Gitee";
    options.DefaultChallengeScheme = "OAuth";
}).AddCookie()
  .AddOAuth("OAuth", "OAuth Scheme", options => 
  {
      options.ClientId = "43e21bdfe93a542a8af3e44396cbe39ef0c9318c4c0bc640c354a16a7372347e";
      options.ClientSecret = "e6be78689cd05f802423ce807b818d015c53a0d36f9b167f138172b638815995";
      options.CallbackPath = new PathString("/signin-oauth");
      options.AuthorizationEndpoint = "https://gitee.com/oauth/authorize";
      options.TokenEndpoint = "https://gitee.com/oauth/token";
      options.UserInformationEndpoint = "https://gitee.com/api/v5/user";
      options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
      options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
      options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
      options.SaveTokens = true; // 保存访问令牌和刷新令牌
      options.Scope.Add("openid");
      options.Scope.Add("profile");
  })
    
    ;

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();
